<?
        $results = array();
        require_once('../inc/orders.php');
        require_once('../inc/orderstatus.php');
		require_once('../inc/shippingmethods.php');
		require_once('../inc/users.php');
        if(isset($_POST['id']))
        {
                $row = $_POST;
                $results = ValidateOrder($row);
                if($results === true)
                        $results = SaveOrder($row);
                if($results === true)
                {
                        if(isset($_REQUEST['ajax'])){
                                $rs = GetOrder($row['id']);
                                include('item.php');
                        }else{
                                header("Location: index.php?inserted=$row[id]#inserted");
                        }
                        die();
                }
        }else{
                if(isset($_REQUEST['id']))
                        $row = GetOrder($_REQUEST['id']);
                else
                        $row = BlankOrder();
        }
        $users = GetUsers();
		$shipping = GetShippingMethods();
		$statuses = GetOrderStatuses();
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Details of Order: <?=$row['id']?></title>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="../inc/bootstrap.css">
    <style type="text/css" >
        .error { color: red; }
                input.error { border-color: red; }
    </style>
</head>

<body>
		<? print_r($row); ?>
        <? include('../inc/header.php'); ?>
        <div class="container">
                        <? include('../inc/nav.php'); ?>
            <div id="contents">
               
                <dl class="error">
                        <? foreach ($results as $key => $value): ?>
                                                <dt><?=$key?></dt>
                                                <dd><?=$value?></dd>
                                        <? endforeach; ?>
                </dl>
               
                <form class="form-horizontal" method="post" action="edit.php">
                    <div class=" control-group">
                        <label class="control-label" for="Transaction">Transaction:</label>
                        <div class="controls">
                            <input      type="text" name="Transaction" id="Transaction" value="<?=$row['Transaction']?>"
                                                                        class="required  <?=isset($results['Transaction']) ? 'error' : '' ?>"
                                                         />
                             <span class="error"><?=isset($results['Transaction']) ? $results['Transaction'] : '' ?></span>
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <label class="control-label" for="Cost">Cost:</label>
                        <div class="controls">
                            <input      type="text" name="Cost" id="Cost" value="<?=$row['Cost']?>"
                                        class="<?=isset($results['Cost']) ? 'error' : '' ?>"
                            />
                             <span class="error"><?=isset($results['Cost']) ? $results['Cost'] : '' ?></span>
                        </div>
                    </div>
                    
                    <div class=" control-group">
                        <label class="control-label" for="ShippingCost">Shipping Cost:</label>
                        <div class="controls">
                            <input      type="text" name="ShippingCost" id="ShippingCost" value="<?=$row['ShippingCost']?>"
                                                                        class="required  <?=isset($results['ShippingCost']) ? 'error' : '' ?>"
                                                         />
                             <span class="error"><?=isset($results['ShippingCost']) ? $results['ShippingCost'] : '' ?></span>
                        </div>
                    </div>
                    
                    <div class=" control-group">
                        <label class="control-label" for="NumItems">Number Of Items:</label>
                        <div class="controls">
                            <input      type="text" name="NumItems" id="NumItems" value="<?=$row['NumItems']?>"
                                                                        class="required  <?=isset($results['NumItems']) ? 'error' : '' ?>"
                                                         />
                             <span class="error"><?=isset($results['NumItems']) ? $results['NumItems'] : '' ?></span>
                        </div>
                    </div>
                    
                    
                    <div class="control-group">
                        <label class="control-label" for="Users_id">User:</label>
                        <div class="controls">
                                <select name="Users_id" id="Users_id" class="<?=isset($results['Users_id']) ? 'error' : '' ?>">
                                <option value="">User</option>
                                        <? while($urow = $users->fetch_assoc()):  ?>
                                        <option
                                        value="<?=$urow['id']?>"
                                        <? if($urow['id'] == $row['Users_id']){ ?>selected='selected'<? } ?>
                                        ><?=$urow['FirstName']?> <?=$urow['LastName']?></option>
                                <? endwhile; ?>
                            </select>
                            <span class="error"><?=isset($results['Users_id']) ? $results['Users_id'] : '' ?></span>
                        </div>
                    </div>
                    
                    <div class="control-group">
                    <label class="control-label" for="ShippingMethods_id">Shipping Method:</label>
                        <div class="controls">
                                <select name="ShippingMethods_id" id="ShippingMethods_id" class="<?=isset($results['ShippingMethods_id']) ? 'error' : '' ?>">
                                <option value="">Shipping Method</option>
                                        <? while($srow = $shipping->fetch_assoc()):  ?>
                                        <option
                                        value="<?=$srow['id']?>"
                                        <? if($srow['id'] == $row['ShippingMethods_id']){ ?>selected='selected'<? } ?>
                                        ><?=$srow['Description']?></option>
                                <? endwhile; ?>
                            </select>
                            <span class="error"><?=isset($results['ShippingMethods_id']) ? $results['ShippingMethods_id'] : '' ?></span>
                        </div>
                    </div>
                    
                    <div class="control-group">
                    <label class="control-label" for="OrderStatus_id">Order Status:</label>
                        <div class="controls">
                                <select name="OrderStatus_id" id="OrderStatus_id" class="<?=isset($results['OrederStatus_id']) ? 'error' : '' ?>">
                                <option value="">Order Status</option>
                                        <? while($orow = $statuses->fetch_assoc()):  ?>
                                        <option
                                        value="<?=$orow['id']?>"
                                        <? if($orow['id'] == $row['OrderStatus_id']){ ?>selected='selected'<? } ?>
                                        ><?=$orow['Description']?></option>
                                <? endwhile; ?>
                            </select>
                            <span class="error"><?=isset($results['OrderStatus_id']) ? $results['OrderStatus_id'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <div class="controls">
                            <input type="hidden" name="id" value="<?=$row['id']?>" />
                                <input class="btn btn-primary" type="submit" value="Save" />
                            <a href="./" class="cancel-link">Cancel</a>
                        </div>
                    </div>
                </form>
           </div>
       </div>
       <script type="application/javascript" src="http://code.jquery.com/ui/1.9.1/jquery-ui.js">
           </script>
       <script type="application/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
           </script>
       <script type="text/javascript">
                        $(function(){
                                //$("#Keyword_id").val('<?=$row['UserType_id']?>');
                                $("input[type='date']").datepicker();
                               
                                $("form").validate({
                                        rules: {
                                                UserType_id: { required: true }
                                        }
                                });
                               
                        });
           </script>
</body>
</html>
